﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HT.Data.BussinessLogic;
using HT.Data.Objects;
using System.Data;
namespace Hotel.UserControls
{
  public partial class Hotel : System.Web.UI.UserControl
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
        LoadData();
    }

    private void LoadData()
    {
      try
      {
        DataSet ds = HotelManger.GetInstance().GetAllHotel(0);
        if (ds.Tables[0].Rows.Count > 0)
        {
          gvHotel.DataSource = ds;
          gvHotel.DataBind();
        }
        else
        {
          DataTable dt = new DataTable();
          dt.Columns.Add("HotelId");
          dt.Columns.Add("Name");
          dt.Columns.Add("Address");
          dt.Columns.Add("StarRating");
          dt.Columns.Add("Email");
          dt.Columns.Add("Phone");
          dt.Columns.Add("Fax");
          dt.Columns.Add("Edit");
          dt.Columns.Add("Delete");
          DataRow dr = dt.NewRow();
          dt.Rows.Add(dr);
          gvHotel.DataSource = dt;
          gvHotel.DataBind();
          gvHotel.Rows[0].Visible = false;
        }
      }
      catch (Exception ex)
      {

      }
    }

    protected void gvHotel_RowEditing(object sender, GridViewEditEventArgs e)
    {
      gvHotel.EditIndex = e.NewEditIndex;
      LoadData();
    }

    protected void gvHotel_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {

      gvHotel.EditIndex = -1;
      LoadData();
    }

    protected void gvHotel_RowCommand(object sender, GridViewCommandEventArgs e)
    {
      if (e.CommandName.Equals("Insert"))
      {
        HotelObject ho = new HotelObject();
        // ho.Id = Convert.ToInt32(gvHotel.DataKeys[e.RowIndex].Values[0].ToString());
        ho.Name = ((TextBox)gvHotel.FooterRow.FindControl("txtNameNew")).Text;
        ho.Address = ((TextBox)gvHotel.FooterRow.FindControl("txtAddress")).Text;
        ho.StarRating = ((TextBox)gvHotel.FooterRow.FindControl("txtRating")).Text;
        ho.Phone = ((TextBox)gvHotel.FooterRow.FindControl("txtPhone")).Text;
        ho.Fax = ((TextBox)gvHotel.FooterRow.FindControl("txtFax")).Text;
        ho.Email = ((TextBox)gvHotel.FooterRow.FindControl("txtEmail")).Text;
        ho.WebSite = ((TextBox)gvHotel.FooterRow.FindControl("txtWebSite")).Text;
        if (!string.IsNullOrEmpty(ho.Name))
        {
          HotelManger.GetInstance().SaveHotel(ho);
          LoadData();
        }
      }

    }

    protected void gvHotel_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
      int id = Convert.ToInt32(gvHotel.DataKeys[e.RowIndex].Values[0].ToString());
      HotelManger.GetInstance().DeleteHotel(id);
      LoadData();
    }

    protected void gvHotel_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
      HotelObject ho = new HotelObject();
      ho.Id = Convert.ToInt32(gvHotel.DataKeys[e.RowIndex].Values[0].ToString());
      ho.Name = ((TextBox)gvHotel.Rows[e.RowIndex].FindControl("txtName")).Text;
      ho.Address = ((TextBox)gvHotel.Rows[e.RowIndex].FindControl("txtAddress")).Text;
      ho.StarRating = ((TextBox)gvHotel.Rows[e.RowIndex].FindControl("txtRating")).Text;
      ho.Phone = ((TextBox)gvHotel.Rows[e.RowIndex].FindControl("txtPhone")).Text;
      ho.Fax = ((TextBox)gvHotel.Rows[e.RowIndex].FindControl("txtFax")).Text;
      ho.Email = ((TextBox)gvHotel.Rows[e.RowIndex].FindControl("txtEmail")).Text;
      ho.WebSite = ((TextBox)gvHotel.Rows[e.RowIndex].FindControl("txtWebSite")).Text;
      HotelManger.GetInstance().UpdateHotel(ho);
      gvHotel.EditIndex = -1;
      LoadData();
    }


  }
}